package com.clip.util;

import javapns.back.PushNotificationManager;
import javapns.back.SSLConnectionHelper;
import javapns.data.Device;
import javapns.data.PayLoad;

public class IosMessagePusher {
	private static String HOSTFORPRODUCT = "gateway.push.apple.com";
	private static String HOSTFORTEST = "gateway.sandbox.push.apple.com";;

	private static int PORT = 2195;

	private static String CERTIFICATEPATH = "D://aps_developer_identity.p12";// 导出的证书
	private static String CERTIFICATEPASSWORD = "111111";// 此处注意导出的证书密码不能为空因为空密码会报错

	public static void pushClipeventAlert(String deviceparam, String content) {
		try {
			String deviceToken = deviceparam;
			deviceToken.replace("\\S", "");
			/** JavaPNS_2.2 **/
			// File file=new File("D://aps_developer_identity.p12");//导出的证书
			// PushedNotifications ps=Push.alert("新的PUSH测试", file, "111111",
			// false, deviceToken);
			// ps.
			/** -- end **JavaPNS_1.6** -- */
			/** JavaPNS_1.6 **/
			PayLoad payLoad = new PayLoad();
			payLoad.addAlert(content);
			payLoad.addSound("default");

			PushNotificationManager pushManager = PushNotificationManager
					.getInstance();
			pushManager.addDevice("iPhone", deviceToken);

			// Connect to APNs
			/************************************************
			 * 测试的服务器地址：gateway.sandbox.push.apple.com /端口2195
			 * 产品推送服务器地址：gateway.push.apple.com / 2195
			 ***************************************************/
			String host = HOSTFORTEST;
			int port = PORT;
			pushManager.initializeConnection(host, port, CERTIFICATEPATH,
					CERTIFICATEPASSWORD,
					SSLConnectionHelper.KEYSTORE_TYPE_PKCS12);

			// Send Push
			Device client = pushManager.getDevice("iPhone");
			pushManager.sendNotification(client, payLoad);
			pushManager.stopConnection();
			pushManager.removeDevice("iPhone");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
